//
//  NRWCarRefuseVC.m
//  New_Runway
//
//  Created by 张记 on 2018/5/3.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarRefuseVC.h"
#import "CarBaseInfoModel.h"
#import "EditCarInfoVC.h"
#import "IamgeFactory.h"
@interface NRWCarRefuseVC ()
@property (nonatomic, strong) UILabel *reasonL;
@property (nonatomic, strong) CarBaseInfoModel *infomodel;

@end

@implementation NRWCarRefuseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"驳回原因";
    self.view.backgroundColor = kColorWhite();
    [self.navigationBarView.rightItem setTitle:@"去入库" forState:UIControlStateNormal];
    [self.view addSubview:self.reasonL];
    kSelfWeak;
    [weakSelf.reasonL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(self.navH+18);
        make.left.equalTo(weakSelf.view).offset(16);
        make.right.equalTo(weakSelf.view).offset(-16);
    }];
    [self loadRefuseReasonDataWithCarId:self.carId];
    [self loadCarBaseInfo];                                                       
}
-(UILabel *)reasonL{
    if (!_reasonL) {
        _reasonL = [[UILabel alloc] init];
        _reasonL.numberOfLines = 0;
        _reasonL.textColor = MAIN_FONT_COLOR();
        _reasonL.font = [UIFont systemFontOfSize:14];
    }
    return _reasonL;
}
-(void)loadRefuseReasonDataWithCarId:(NSString *)carId{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,carRefuseApi];
    NSDictionary *params = @{@"carId":carId,@"carStateKey":@"carStockState",@"carStateValue":@1};
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        NSArray *dataArray = response[@"result"];
        if (dataArray.count != 0) {
            NSDictionary *dic = [dataArray lastObject];
            _reasonL.text = dic[@"carLogInfo"];
        }
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败请检查网络" duration:2.0 withRemindId:remindFail];
    }];
 
    
}
-(void)loadCarBaseInfo{
    
    [SVProgressHUD show];
    [YQNetworking postWithUrl:[NSString stringWithFormat:@"%@%@",Api,carDetailApi] refreshRequest:YES cache:NO params:@{@"carId":self.carId,@"imageViewId":@(2)} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        _infomodel = [CarBaseInfoModel mj_objectWithKeyValues:response];
        if (_infomodel.code == 1) {
            for (NSDictionary *dic in _infomodel.result.carImages) {
                CarCarimages *imagemodel =  [CarCarimages mj_objectWithKeyValues:dic];
                [self saveCarImageUrl:imagemodel.imageKey imageUrl:imagemodel.imageUrl];
            }
        }
        [SVProgressHUD dismiss];
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败请检查网络" duration:2.0 withRemindId:remindFail];
        [SVProgressHUD dismiss];
        
    }];
    
}
-(void)rightItemClick{
    EditCarInfoVC *editVc = [[EditCarInfoVC alloc] init];
    editVc.infomodel = self.infomodel;
    editVc.selIndex = self.selePath;
    editVc.titleL = @"申请入库";
    editVc.isReject = 1;
    [self.navigationController pushViewController:editVc animated:YES];
    
}
-(void)saveCarImageUrl:(NSString *)imageKey imageUrl:(NSString *)imageUrl{
    IamgeFactory *iamgeUser = [IamgeFactory sharedMemory];
    [iamgeUser setValue:imageUrl forKey:imageKey];
    [iamgeUser save];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
